<?php
defined('BASEPATH') or die('forbidden');

class Payment_Controller Extends Controller {

	private $inc;
	private $productidlist;	//产品id
	
	function _init() {//print_r($_SESSION['tttttkk']);
		check_level(50, segment(0), segment(1));

		$this->productidlist = array(
				"1"=>"30元月卡充值 ",
				"2"=>"198元十年卡充值 ",
				"3"=>"6元钻石卡充值 ",
				"4"=>"30元钻石卡充值 ",
				"5"=>"68元钻石卡充值 ",
				"6"=>"198元钻石卡充值 ",
				"7"=>"328元钻石卡充值 ",
				"8"=>"648元钻石卡充值 ",
		);

        //得到选择的区号
        $areanum = $_SESSION['area_selected_id'];
        if ($areanum == '')
        {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
            return;
        }

        //query server mapping
        $selectSql = "SELECT * FROM server_mapping WHERE dbtype='1' AND areanum={$areanum} ";
        //$selectSql = "SELECT * FROM server_mapping WHERE dbtype='1'";
        $this->db->query($selectSql);
        $this->server_info=$this->db->get_array('default');

        //var_dump($this->server_info);
        // db数据库
        $this->m = new MongoClient("mongodb://{$this->server_info['username']}:{$this->server_info['userpwd']}@{$this->server_info['dbip']}");
	}

	function index() {

		$this->view->display("dbvip/payment/index");
	}

	function flexigrid() {
		    
		list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $query=$_POST['query'];
        $query_type=$_POST['qtype'];
        if($query_type=='_id'){
            $filter=array($query_type=>(float)$query);
        }else{
            $filter=array($query_type=>$query);
        }
        $collection=$this->m->{$this->server_info['dbname']}->PlayerReCharge;
        if($query!=''){
            $document=$collection->find($filter);
        }else{
            $document=$collection->find();
        }
        $data=array();
        $total = count($document);

        foreach ($document as $key=>$value){
            $value['createtime'] = get_datetime($value['createtime']);
            //foreach ($value as $k => $v) {
            //    if $k == 'status'{
            //    	$value2['status'] = "支付失败未发货";

             //   	if $v == 0{
             //   		$value2['status'] = "未支付";
             //   	}elseif $v == 2{
            //    		$value2['status'] = "已支付并发货";
             //   	}
             //   } elseif $k == 'moneytype'{
             //   	$value2['status'] = "其他货币";
            //    	if $v == 1{
             //   		$value2['status'] = "人民币";
            //    	}
            //    }else{
            //    	$value2['status'] = $value['status'];
             //   }
           // }
            if ($value['status'] == '2'){
            	$value['status'] = "已支付并发货";
            }elseif ($value['status'] == '0'){
            	$value['status'] = "未支付";
            }else{
            	$value['status'] = "支付失败未发货";
            }

            if ($value['moneytype'] == '1'){
                $value['moneytype'] = "人民币";
            }else{
            	 $value['moneytype'] = "其他货币";
            }
            
            //$value['loginday'] = get_datetime($value['loginday']);
            $data[$key]=array('id' => $value['_id'], 'cell' => $value);
        }

		$result = array(
	            'page' => $page,
	            'total' => $total,
	            'rows' => $data
		);
		 
		//$this->payment_db->free_all();
		echo json_encode($result);
	}
	
	function edit($id)
	{
		if (!$id)
		{
			error(translate('C_INVALID_PARAM_ID', $id));
		}
		$row = $this->payment_db->one("SELECT * FROM {pre}payment WHERE id={$id}");
		if (!is_array($row))
		{
			error(translate('C_INVALID_PARAM_ID', $id));
		}
		if (is_submit())
		{
			//print_r($_POST);
			$rowdata = $_POST['row'];
			//print_r($rowdata);
			
			require_once APPPATH . 'channels/Testpayment.php';
			$test = new Testpayment();
			$res = $test->pay($rowdata);

			if(true == $res)
			{
				echo "success";
			}else 
			{
				echo "failed";
			}
			
		}else
		{
			//对金额进行处理下
			if($row['money']==null)
			{
				$row['money'] = 0;
			}
			$this->view->display("dbvip/payment/edit", array('row' => $row));
		}
	}
}

?>